package com.yunxin.core.infra.persistence.repository.merchant;

import com.yunxin.core.domain.model.merchant.MerchantUserAccount;
import com.yunxin.core.domain.model.merchant.MerchantUserAccountRepository;
import com.yunxin.core.infra.persistence.converter.MerchantConverter;
import com.yunxin.core.infra.persistence.entity.merchant.MerchantUserAccountDO;
import com.yunxin.core.infra.persistence.mapper.merchant.MerchantUserAccountExtendMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author zero°
 * @since 2023-07-12
 */
@Repository
@RequiredArgsConstructor
public class MerchantUserAccountRepositoryImpl implements MerchantUserAccountRepository {

    private final MerchantUserAccountExtendMapper merchantUserAccountExtendMapper;

    private final MerchantConverter merchantConverter;

    @Override
    public boolean saveMerchantAccount(MerchantUserAccount merchantUserAccount) {
        MerchantUserAccountDO merchantUserAccountDO = merchantConverter.formMerchantUserAccount(merchantUserAccount);
        int count = merchantUserAccountExtendMapper.insertSelective(merchantUserAccountDO);
        return count > 0;
    }

    @Override
    public boolean updateMerchantAccount(MerchantUserAccount merchantUserAccount) {
        MerchantUserAccountDO merchantUserAccountDO = merchantConverter.formMerchantUserAccount(merchantUserAccount);
        int count = merchantUserAccountExtendMapper.updateByPrimaryKeySelective(merchantUserAccountDO);
        return count > 0;
    }

    @Override
    public MerchantUserAccount getMerchantUserAccountBySysUserId(Long sysUserId) {
        MerchantUserAccountDO merchantUserAccountDO = merchantUserAccountExtendMapper.selectBySysUserId(sysUserId);
        return merchantConverter.toMerchantUserAccount(merchantUserAccountDO);
    }

    @Override
    public boolean deleteMerchantAccountBySysUserIds(List<Long> sysUserIds) {
        int count = merchantUserAccountExtendMapper.updateMerchantAccountIsDelBySysUserIds(sysUserIds);
        return count > 0;
    }
}
